<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://"
      + request.getServerName() + ":" + request.getServerPort()
      + path + "/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
<base href="<%=basePath%>">
<!-- 下拉框 -->
<link rel="stylesheet" href="static/ace/css/chosen.css" />

<link href="plugins/kindeditor/themes/simple/simple.css" type="text/css" rel="stylesheet">
<link href="plugins/kindeditor/themes/default/default.css" rel="stylesheet">
<link href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet">
<link href="static/styles/base.css" rel="stylesheet">
<style>td{vertical-align: middle!important;}
</style>
<!-- jsp文件头和头部 -->
<%@ include file="../index/top.jsp"%>
</head>
<body class="no-skin">
  <!-- /section:basics/navbar.layout -->
  <div class="main-container" id="main-container">
    <!-- /section:basics/sidebar -->
    <div class="main-content">
      <div class="main-content-inner">
        <div class="page-content">
          <div class="row">
            <div class="col-xs-12">
                <form action="period/${msg }.do" name="goodsForm" id="goodsForm" method="post">
                  <div id="zhongxin" style="padding-top: 13px;">
                   <input type="hidden" name="countId" id="countId" />
                   <input type="hidden" id="count" value="${spec.area_goods_count_id }" />
                   <input type="hidden" name="specTimeId" id="specTimeId" value="${pd.specTimeId }" />
                   <input type="hidden" id="goodsId" value="${pd.goodsId }" />
                   <input type="hidden" id="timeId" value="${pd.timeId }" />
                   <input type="hidden" id="specId" value="${spec.goods_spec_id }" />
                   <input type="hidden" id="base_id" value="${pd.base_id }" />
                   <input type="hidden" id="pic" value="${spec.picture}" />
                   <input type="hidden" id="num" value="${num }"/>
                  <table id="table_report1" class="table table-striped table-bordered table-hover">
                    <tr>
                       <td
                          style="width:100px; text-align: right; padding-top: 13px;">绑定商品:</td>
                        <td>
                          <a href="javascript:void(0)" onclick="areaGoods();">选择区域商品</a>
                           <span id="gS"></span>
                        </td>
                      </tr>
                      <tr>
                        <td style="width:100px;text-align: right;padding-top: 13px;">库存:</td>
                        <td><input type="number" name="stock_num" id="stock_num" value="${spec.stock_num }" maxlength="11" min="0" style="width:98%;"/></td>
                      </tr>
                      <tr>
                        <td style="width:100px;text-align: right;padding-top: 13px;">库存警告:</td>
                        <td><input type="number" name="stock_warn_num" id="stock_warn_num" value="${spec.stock_warn_num }" maxlength="11" min="0"  style="width:98%;"/></td>
                      </tr>
                      <tr>
                        <td style="width:100px;text-align: right;padding-top: 13px;">销售价:</td>
                        <td><input type="number" name="price" id="price" value="${spec.price }" maxlength="11" min="0" style="width:98%;"/></td>
                      </tr>
                      <tr>
                        <td style="width:100px;text-align: right;padding-top: 13px;">商品已售数量:</td>
                        <td><input type="number" name="sale_num" id="sale_num" value="${spec.sale_num }" maxlength="11" min="0" style="width:98%;"/></td>
                      </tr>
                      <tr>
                        <td style="width:100px;text-align: right;padding-top: 13px;">最低起售数量:</td>
                        <td><input type="number" name="min_shop_num" id="min_shop_num" value="${spec.min_shop_num }" maxlength="11" min="0" style="width:98%;"/></td>
                      </tr>
                      <tr>
                        <td style="width:100px;text-align: right;padding-top: 13px;">最高起售数量:</td>
                        <td><input type="number" name="max_shop_num" id="max_shop_num" value="${spec.max_shop_num }" maxlength="11" min="0" style="width:98%;"/></td>
                      </tr>
                       <tr>
                        <td
                          style="width:110px; text-align: right; padding-top: 13px;">商品权益周期 (月):</td>
                        <td><input type="number" name="equity_cycle" id="equity_cycle" value="${spec.equity_cycle }" maxlength="11" min="0" style="width:98%;"/></td>
                      </tr>
                       <tr>
                        <td
                          style="width:100px; text-align: right; padding-top: 13px;">截止使用时间:</td>
                        <td><span class="dib vm">截止使用时间:&nbsp;</span><input id="end_use_time" name="end_use_time" placeholder="请选择预计收获时间" value="${spec.end_use_time }" /></td>
                      </tr>
                     <%--  <tr>
                        <td
                          style="width: 30px; text-align: right; padding-top: 13px;">销售时间:</td>
                        <td><span class="dib vm">上架时间:&nbsp;</span><input
                          id="start_time" name="start_time"
                          placeholder="请选择上架时间"
                          value="${spec.start_time }" />&emsp;&emsp; <span
                          class="dib vm">下架时间 :&nbsp;</span><input
                          class="dib vm" id="end_time" name="end_time"
                          placeholder="请选择下架时间 " value="${spec.end_time }" /></td>
                      </tr> --%>
                     <%--  <tr>
                        <td
                          style="width: 100px; text-align: right; padding-top: 13px;">展示图片:</td>
                        <td><a href="javascript:void(0)"
                          class="picFileUpload">上传图片</a> <input
                          type="hidden" id="picture" name="picture"
                          value="${spec.picture }" /></td>
                      </tr>
                       <tr>
                       <td
                          style="width:100px; text-align: right; padding-top: 13px;">还原图片:</td>
                        <td>
                          <a href="javascript:void(0)" class="reduction">一键还原 </a>
                           &nbsp;&nbsp;&nbsp;(商品图片)
                        </td>
                      </tr>   --%>                    
                      </table>
                      <table id="table_report2" class="table table-striped table-bordered table-hover">
                       <c:if test="${harvestList !=null && harvestList !='' && harvestList !='[]'}">
                        <thead>
                         <tr>
                        <td class="w100 tr">添加收获参数</td>
                         <td>
                           <a class="ion-ios-plus-outline f20 dib btnAddHarvest" id="plus" href="javascript:;"></a>
                        </td>
                      </tr>
                       </thead>
                       <tbody class="harvest">
                        <c:choose>
                         <c:when test="${not empty harvestList}">
                        <c:forEach items="${harvestList}" var="p" varStatus="vs">
                              
                              <tr>
                              <td class="w100 tr">第${vs.index+1}阶段:</td>
                              <td>
                                <div class="container-fluid">
                                  <div class="row">
                                  <div class="dib vm"><span class="ion-ios-minus-outline btn-minus f20 dib btnDeleteHarvest"></span></div>
                                    <div class="dib vm w300 ml20">
                                    <input type="text" class="form-control" name="yields" placeholder="请输入预计产量,只支持数字" value="${p.yields }">
                                    </div>
                                    <div class="dib vm w300 ml20">
                                    <input type="text" class="form-control times" id="times1" name="times" value="${p.times }" placeholder="请选择收获时间节点">
                                    </div>
                                  </div></div>
                                </td>
                               </tr>
                        </c:forEach>
                        </c:when>
                        </c:choose>
                        </tbody>
                      </c:if>
                    <c:if test="${harvestList ==null || harvestList =='[]'}">
                     <thead>
                         <tr>
                        <td class="w100 tr">添加收获参数</td>
                         <td>
                           <a class="ion-ios-plus-outline f20 dib btnAddHarvest" id="plus" href="javascript:;"></a>
                        </td>
                      </tr>
                       </thead>
                      <tbody class="harvest">
                      <tr>
                      <td class="w100 tr aa"></td>
                      <td>
                        <div class="container-fluid">
                          <div class="row">
                          <div class="dib vm"><span class="ion-ios-minus-outline btn-minus f20 dib btnDeleteHarvest"></span></div>
                            <div class="dib vm w300 ml20">
                            <input type="text" class="form-control" name="yields" placeholder="请输入预计产量,只支持数字">
                            </div>
                            <div class="dib vm w300 ml20">
                            <input type="text" class="form-control times" id="times1" name="times" placeholder="请选择收获时间节点">
                            </div>
                          </div></div>
                        </td>
                       </tr>
                      </tbody>
                      </c:if>
                      <tfoot>
                       <tr>
                        <td style="text-align: center;" colspan="10">
                          <a class="btn btn-mini btn-primary" onclick="save();">保存</a>
                          <a class="btn btn-mini btn-danger" onclick="top.Dialog.close();">取消</a>
                        </td>
                      </tr>
                      </tfoot>
                      </table>
                  </div>
                  <div id="zhongxin2" class="center" style="display:none"><br/><br/><br/><br/><img src="static/images/jiazai.gif" /><br/><h4 class="lighter block green"></h4></div>
                </form>
            </div>
            <!-- /.col -->
          </div>
          <!-- /.row -->
        </div>
        <!-- /.page-content -->
      </div>
    </div>
    <!-- /.main-content -->
  </div>
  <!-- /.main-container -->
  <!-- basic scripts -->
  <!-- 页面底部js¨ -->
  <%@ include file="../index/foot.jsp"%>
  <!-- ace scripts -->
  <script src="static/ace/js/ace/ace.js"></script>
  <!-- inline scripts related to this page -->
   <script src="static/ace/js/bootbox.js"></script>
  <!-- 下拉框 -->
  <script src="static/ace/js/chosen.jquery.js"></script>
  <!--提示框-->
  <script type="text/javascript" src="static/js/jquery.tips.js"></script>
  <script type="text/javascript" src="static/js/common/file_upload.js"></script>
  <script type="text/javascript" src="plugins/kindeditor/kindeditor-all.js"></script>
  <script type="text/javascript" src="plugins/kindeditor/lang/zh_CN.js"></script>
  <script type="text/javascript" src="static/laydate/laydate.js"></script>
  <script type="text/javascript" src="static/js/provincial.municipal.league.js"></script>
</body>
<script>
	//添加收获参数
	$(function(){
		$(".aa").text("第1阶段")
		var $harvest = $('.harvest')
		var initNum=1
		var initNum1=1
		$harvest.on('click','.btnDeleteHarvest',function(){
			var tr =  $harvest[0].getElementsByTagName('tr');
			if(tr.length==1)return;
			var parent = $(this).parents('tr')
			parent.remove()
			initNum-=1;
			
			var items = $harvest.children()
			
			for(var i=0;i<items.length;i++){
				items.eq(i).find('.aa').html('第'+(i+1)+'阶段')
				
			}
			
			
		})
		
		$('.btnAddHarvest').on('click',function(){
			var len = $harvest.children().length
			if(len>=3)return;
			initNum+=1;
			var newEle = $harvest.children().eq(0).clone()
			newEle.find('.aa').html('第'+initNum+'阶段')
			newEle.find('input').val('')
			newEle.find('.times').attr('id','times'+initNum)
			$harvest.append(newEle)
			setTimeout(function(){
				setTime('#times'+initNum);
			},0)
		})
		
	});
	
	
</script>
<script type="text/javascript">
  $(top.hangge());
  //页面初始化完毕后执行此方法
  
    $(function(){
    	var count=$("#count").val();
    	if(count !=""){
    		var num=$("#num").val();
    		$("#countId").val(count);
    		$("#stock_num").attr("max",num)
    		 $("#gS").html("已绑定");
    	}
      TAOTAO.kingEditorParams = {
              //指定上传文件参数名称
              filePostName  : "uploadFile",
              //指定上传文件请求的url。
              uploadJson : '<%=basePath%>pic/upload',
              //上传类型，分别为image、flash、media、file
              dir : "image",
              extraFileUploadParams:{isCom:"01",width:800,height:800,quality:0.8,end:"view"}
            }
            
            TAOTAO.kingEditorAutoParams = {
                //指定上传文件参数名称
                filePostName  : "uploadFile",
                //指定上传文件请求的url。
                uploadJson : '<%=basePath%>pic/upload',
                //上传类型，分别为image、flash、media、file
                dir : "image",
                extraFileUploadParams:{isCom:"01",width:200,height:200,quality:1,end:"thumb"}
              }
  
  });
     TAOTAO.initPicUpload($("input[name=picture]").val());
     $('body').on('click','.imageButton',function(){
         var id=$("#id").val();
         var picture=$("#picture").val();
         var imgsrc=$(this).parent('li').find("img").attr("src");
         var _this = $(this);
         var newPicture="";
         if(picture.indexOf((imgsrc+',')) >-1){
           newPicture=picture.replace((imgsrc+','),'');
         }else if(picture.indexOf((','+imgsrc)) > -1){
           newPicture=picture.replace((','+imgsrc),'');
         }
         $.ajax({
           url:"<%=basePath%>pic/pictureDelete",
           type:"post",
           async:true,
           dataType:"text",
           data:{"id":id,"newPicture":newPicture,"imgsrc":imgsrc},
           success : function(data) {
             if(data != null){
            	 
               _this.parent('li').remove();
               $("#picture").val(newPicture);
             }
             alert(message);
           },error : function(XMLHttpRequest, textStatus){
             alealert(textStatus);
           }
         });   
       });
     
    
    $(".reduction").click(function(){
    	var pic=$("#pic").val();
        $("#picture").val(pic);
        TAOTAO.initPicUpload($("input[name=picture]").val());
	});
	
    function areaGoods(){
		var timeId= $("#timeId").val();
		var specId= $("#specId").val();
		var baseId= $("#base_id").val();
		var countId= $("#countId").val();
		
		  var diag = new top.Dialog();
		     diag.Drag=true;
		     diag.Title ="绑定区域商品";
		     diag.URL = '<%=basePath%>period/bindingGoods.do?specId='+specId+'&baseId='+baseId+'&countId='+countId;
		    diag.Width = 700;
		    diag.Height = 400;
		    diag.CancelEvent = function() {
		      diag.close();
		    }, diag.OKEvent = function() {
		      var child = diag.innerFrame.contentWindow.document;
		      var countId=$(child).find("#ids").val();
		      var num=$(child).find("#num").val();
		      if(countId!=""||num!=""){
		    	  $("#num").val(num);
		    	  $("#stock_num").attr("max",num)
			      $("#countId").val(countId);
			       $("#gS").html("已绑定");
		      }
		      diag.close();
		    };//点击确定后调用的方法
		    diag.show();
	};
    
    function save(){
    	var countId =$("#countId").val()
    	var goodsId =$("#goodsId").val()
    	var timeId =$("#timeId").val()
    	var specId =$("#specId").val()
    	var stock_num =$("#stock_num").val()
    	var stock_warn_num =$("#stock_warn_num").val()
    	var price =$("#price").val()
    	var sale_num =$("#sale_num").val()
    	var min_shop_num =$("#min_shop_num").val()
    	var max_shop_num =$("#max_shop_num").val()
    	var end_use_time =$("#end_use_time").val()
    	var equity_cycle=$("#equity_cycle").val()
    	var startTime=$("#start_time").val()
    	var endTime=$("#end_time").val()
    	var picture=$("#picture").val()
    	var num=$("#num").val()
    	
      if($.trim($("#stock_num").val()) == ""){
    	  alert("请输入库存!")
    	  return false;
      }
    	 if(countId !=""){
    		if((stock_num-num)>0){
    	     	  alert("此商品库存已到达上限!")
    	     	  return false;
    	    }
    	 }else{
    		 $("#countId").val("aa");
    	 }
      if($.trim($("#stock_warn_num").val()) == ""){
    	  alert("请输入商品库存警告数量!")
    	  return false;
      }
      if($.trim($("#price").val()) == ""){
    	  alert("请输入销售价!")
    	  return false;
      }
      if($.trim($("#sale_num").val()) == ""){
    	  alert("请输入商品已售数量!")
    	  return false;
      }
      if($.trim($("#min_shop_num").val()) == ""){
    	  alert("请输入最低起售数量!")
    	  return false;
      }
      if($.trim($("#max_shop_num").val()) == ""){
    	  alert("请输入最高起售数量!")
    	  return false;
      }
      if((max_shop_num-min_shop_num)<0){
    	  alert("最高起售数量要大于最低起售数量")
    	  return false;
      }
      if($.trim($("#equity_cycle").val()) == ""){
    	  alert("请输入权益周期!")
    	  return false;
      }
      if($.trim($("#end_use_time").val()) == ""){
    	  alert("请选择商品截止使用时间!")
    	  return false;
      }
      /*  if($.trim(startTime)==""){
          alert("请选择商品上架时间！");
          return false;
        }
	  if($.trim(endTime)==""){
          alert("请选择商品下架时间！");
          return false;
        }  */
     
  	  if(countId == "" || countId =="aa"){
        	bootbox.confirm("还未绑定区域商品，确定提交吗？", function(result) {
        		if(result) {
        		  $("#goodsForm").submit();
        	      $("#zhongxin").hide();
        	      $("#zhongxin2").show();
        		}
            });
        }else{
      	  $("#goodsForm").submit();
  	      $("#zhongxin").hide();
  	      $("#zhongxin2").show();
        }
      }
    
    var endUseTime = {
		    elem : '#end_use_time',
		    format : 'YYYY-MM-DD hh:mm:ss',
		    min : laydate.now(),
		    max : '2099-06-16 23:59:59',
		    istime : true,
		    istoday : true,
		    choose : function(datas) {
		    }
		  };
    
     var times = {
		    elem : '#times1',
		    format : 'YYYY-MM-DD',
		    min : laydate.now(),
		    max : '2099-06-16',
		    istime : true,
		    istoday : true,
		    choose : function(datas) {}
		  };
    
  laydate(times); 
  laydate(endUseTime);
  
  function setTime(ele){
	  laydate({
	    elem : ele,
	    format : 'YYYY-MM-DD',
	    min : laydate.now(),
	    max : '2099-06-16',
	    istime : true,
	    istoday : true,
	    choose : function(datas) {}
	  });
  }
  
</script>
</html>